package com.sdy.mvc.utils;

import com.alibaba.druid.filter.logging.LogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class DruidManager {
    @Autowired
    private LogFilter logFilter;
    @Autowired
    private StatFilter statFilter;

    public DruidDataSource createDruidDataSource(String url, String driver, String username, String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setProxyFilters(Arrays.asList(logFilter, statFilter));
        return dataSource;
    }
}
